package com.xmim.core.session;

import java.util.Map;

public class DefaultSessionHandlerContext implements SessionHandleContext {

    volatile String name;
    volatile SessionPipeline pipeline;
    volatile DefaultSessionHandlerContext next;
    volatile SessionHandle handler;

    public DefaultSessionHandlerContext(SessionPipeline pipeline, String name, SessionHandle handler) {
        this.pipeline = pipeline;
        this.name = name;
        this.handler = handler;
    }


    @Override
    public String name() {
        return name;
    }

    @Override
    public SessionHandle handler() {
        return handler;
    }

    @Override
    public SessionPipeline pipeline() {
        return pipeline;
    }

    @Override
    public void fireAdded() throws Exception {
        invokeAdded();
    }

    @Override
    public void fireRemoved() throws Exception {
        invokeRemoved();
    }

    @Override
    public void fireRead(Object msg) throws Exception {
        invokeRead(msg);
    }

    @Override
    public void fireClose() throws Exception {
        invokeClose();
    }

    @Override
    public void fireHeartbeat(Object msg) throws Exception {
        invokeHeartbeat(msg);
    }

    @Override
    public void fireException(Throwable cause) {
        invokeException(cause);
    }

    @Override
    public void fireReadComplete() throws Exception {
        invokeReadComplete();
    }

    @Override
    public void fireWriteComplete() throws Exception {
        invokeWriteComplete();
    }

    private void invokeAdded() {
        DefaultSessionHandlerContext context = this.findContext();
        if (context != null && context.handler != null) {
            try {
                context.handler.handleAdded(context);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void invokeRemoved() {
        DefaultSessionHandlerContext context = this.findContext();
        if (context != null) {
            try {
                context.handler.handleRemoved(context);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void invokeRead(Object msg) {
        DefaultSessionHandlerContext context = this.findContext();
        if (context != null) {
            try {
                context.handler.handleRead(context, msg);
            } catch (Exception e) {
                context.handler.handleException(context, e);
            }
        }
    }

    private void invokeClose() {
        DefaultSessionHandlerContext context = this.findContext();
        if (context != null) {
            try {
                context.handler.handleClose(context);
            } catch (Exception e) {
                context.handler.handleException(context, e);
            }
        }
    }

    private void invokeHeartbeat(Object msg) {
        DefaultSessionHandlerContext context = this.findContext();
        if (context != null) {
            try {
                context.handler.handleHeartbeat(context, msg);
            } catch (Exception e) {
                context.handler.handleException(context, e);
            }
        }
    }

    private void invokeException(Throwable cause) {
        DefaultSessionHandlerContext context = this.findContext();
        if (context != null) context.handler.handleException(context, cause);
    }

    private void invokeReadComplete() {
        DefaultSessionHandlerContext context = this.findContext();
        if (context != null) {
            try {
                context.handler.handleReadComplete(context);
            } catch (Exception e) {
                context.handler.handleException(context, e);
            }
        }
    }

    private void invokeWriteComplete() {
        DefaultSessionHandlerContext context = this.findContext();
        if (context != null) {
            try {
                context.handler.handleWriteComplete(context);
            } catch (Exception e) {
                context.handler.handleException(context, e);
            }
        }
    }

    static void invokeAdded(DefaultSessionHandlerContext context) {
        context.invokeAdded();
    }

    static void invokeRemoved(DefaultSessionHandlerContext context) {
        context.invokeRemoved();
    }

    static void invokeRead(DefaultSessionHandlerContext context, Object msg) {
        context.invokeRead(msg);
    }

    static void invokeClose(DefaultSessionHandlerContext context) {
        context.invokeClose();
    }

    static void invokeHeartbeat(DefaultSessionHandlerContext context, Object msg) {
        context.invokeHeartbeat(msg);
    }

    static void invokeException(DefaultSessionHandlerContext context, Throwable cause) {
        context.invokeException(cause);
    }

    static void invokeReadComplete(DefaultSessionHandlerContext context) {
        context.invokeReadComplete();
    }

    static void invokeWriteComplete(DefaultSessionHandlerContext context) {
        context.invokeWriteComplete();
    }

    private DefaultSessionHandlerContext findContext() {
        return this.next;
    }

}
